import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/home',
    name: 'home',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/HomeView.vue'),
   children:[
     {
      path: '/users',
      name: 'users',
      component: () => import(/* webpackChunkName: "about" */ '../views/users.vue'),
     },
     {
      path: '/roles',
      name: 'roles',
      component: () => import(/* webpackChunkName: "about" */ '../views/roles.vue'),
     },
     {
      path: '/rights',
      name: 'rights',
      component: () => import(/* webpackChunkName: "about" */ '../views/rights.vue'),
     },
     {
      path: '/goods',
      name: 'goods',
      component: () => import(/* webpackChunkName: "about" */ '../views/goods.vue'),
     }
   ]
  }
]

const router = new VueRouter({
  routes
})
const whiteList = ['/login','/test']

router.beforeEach((to, from, next) => {
    let token = localStorage.getItem('token')
    if (token) {
      next()
    }
    else {
      if (whiteList.indexOf(to.path) != -1) {
        next()
      }
      else {
        next('/login')
      }
    }
  })
export default router
